// pages/shop/info.js
var app = getApp();
Page({
  onLoad: function (options) {
    var token = wx.getStorageSync('token');
    var appInfo = wx.getStorageSync('app_info');

    if (!token) {
      app.util.newLogin(app);
    }

    var that = this;
    app.util.appInfo(app, function(res){
      that.setData({
        app_name: res.result.name,
        app_kfphone: res.result.kfphone,
        app_kfwechat: res.result.kfwechat
      })
    });
  },
  chooseImage: function () {
    var that = this;

    wx.chooseImage({
      count: 1,
      sizeType: ['compressed'],
      sourceType: ['album', 'camera'],
      success: function (res) {
        var tempFilePaths = res.tempFilePaths;
        wx.showLoading({title: '准备上传...', mask: true});
        app.func.upload('file/upload', tempFilePaths[0], {}, function(res){
          var res =  JSON.parse(res);
            if(res.code == 'success'){
              that.setData({
                logo: res.result[0]
              });

              wx.showToast({
                title: '上传成功',
                icon: 'success',
                duration: 2000
              })
            } else {
              wx.showToast({
                title: '上传失败',
                icon: 'success',
                duration: 2000
              })
            }
        },{
          token: wx.getStorageSync('token')
        });
      }
    });
  },
  save: function (e) {
    var errMsg = '';
    var isReturn = false;

    if(!this.data.logo){
      errMsg = 'LOGO不能为空';
      isReturn = true;
    }

    if(e.detail.value.name.length < 3){
      errMsg = '店铺名不少于3个字符';
      isReturn = true;
    }

    if (!e.detail.value.addr.trim()) {
      errMsg = '地址不能为空';
      isReturn = true;
    }

    if (!e.detail.value.phone.trim()) {
      errMsg = '请授权手机号';
      isReturn = true;
    }

    if(e.detail.value.memo.length < 10){
      errMsg = '申请理由不少于10个字';
      isReturn = true;
    }

    if(errMsg.length > 0 && isReturn){
      wx.showModal({
        title: '提示',
        content: errMsg,
        showCancel: false
      });

      return;
    }

    if (this.data.is_submit) {
      return; 
    } else {
      this.setData({
        is_submit: true
      })
    }

    var that = this;
    wx.showLoading({ title: '提交中...', mask: true });
    app.func.req('shops/create', {
      name: e.detail.value.name,
      addr: e.detail.value.addr,
      phone: e.detail.value.phone,
      memo: e.detail.value.memo,
      logo: that.data.logo,
      form_id: e.detail.formId
    }, 'POST', function (res) {
      that.setData({
        is_submit: false
      })
      wx.hideLoading();
      var msg = '';
      if (res.code == 'success') {
        var shop = res.result;
        if (shop.length == 0) {
          msg = '提交成功，等待审核，约3个工作日';
        } else {
          msg = '申请成功，请在首页点击商户登录';
        }

        wx.showModal({
          title: '提示',
          content: msg,
          showCancel: false,
          success: function(){
            wx.redirectTo({
              url: '/pages/index/home',
            })
          }
        });
      } else {
        msg = '提交失败';
        if(res.msg == 'shop.name.or.phone.exist'){
          msg = '店铺名或手机号已存在';
        }
        wx.showModal({
          title: '提示',
          content: msg,
          showCancel: false
        });
      }
    }, {
        'token': wx.getStorageSync('token')
      });
  },
  phoneBlur: function(e){
    this.setData({
      phone: e.detail.value
    });
  },
  onShareAppMessage: function () {
    var that = this;
    return {
      title: that.data.app_name + '，店铺申请',
      path: '/pages/common/shop_form'
    }
  },
  telPhone: function () {
    var that = this;
    wx.makePhoneCall({
      phoneNumber:  that.data.app_phone
    })
  },
  getPhoneNumber: function (e) {
    var that = this;
    console.log(e.detail.errMsg)
    if (e.detail.errMsg == 'getPhoneNumber:ok') {
      app.func.req("users/auth_phone", {
        encryptedData: e.detail.encryptedData,
        iv: e.detail.iv
      }, "POST", function (res) {
        if (res.code == 'success') {
          that.setData({
            phone: res.result
          });
        }
      }, {
        'token': wx.getStorageSync('token')
      });
    }
  }
})
